//
//  DYZSYuyueViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/6.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit

class DYZSYuyueViewController: SYBaseViewController , UITableViewDelegate, UITableViewDataSource{
    
    var param: [String: String] = [:]
    var model: DYZSDetailModel!
    var selectIndex: Int!
    var selectTime: String!
    var endTime: String!
    var dayNum: Int!
    var roomNum: Int = 1 {
        didSet {
            self.tableView.reloadRows(at: [IndexPath(row: 2, section: 0)], with: .none)
            self.tableView.reloadRows(at: [IndexPath(row: 4, section: 0)], with: .none)
            self.tableView.reloadRows(at: [IndexPath(row: 5, section: 0)], with: .none)
            self.alert.configure(self.model, self.selectIndex, self.selectTime, self.dayNum)
            self.setBottomPrice()
        }
    }
    var bottom: DYYuyueBottomView!
    let alert = DYOrderDetailView()
    let renyang: UIView = {
        let v1: UIView  = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 173.autoWidth()))
        v1.backgroundColor = UIColor(hex: 0x367FF6)

        let v: UIView  = UIView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: 173.autoWidth()-NAV_HEIGHT))
        v.sy_gradLayerCreatWithFrame(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 173.autoWidth()-NAV_HEIGHT), topC: UIColor(hex: 0xF0F0F0, alpha: 0), bottomC: UIColor(hex: 0xF0F0F0, alpha: 1))
//        
        v1.addSubview(v)
        return v1
    }()
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 17.autoBoldFontSize()
        v.text = "南京平山荣逸民宿"
        return v
    }()
    
    var back: UIButton = {
        let v: UIButton = UIButton()
        v.setImage(UIImage(named: "nav_back"), for: .normal)
        return v
    }()
    
    
    var tableView: UITableView!
    
    init(model: DYZSDetailModel, selectIndex: Int, selectTime: String, endTime: String, dayNum: Int) {
        super.init(nibName: nil, bundle: nil)
        self.model = model
        self.selectIndex = selectIndex
        self.selectTime = selectTime
        self.dayNum = dayNum
        self.endTime = endTime
        self.param["roomId"] = self.model.roomList?[selectIndex].id
        self.param["startDate"] = selectTime
        self.param["endDate"] = endTime
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTopView()
        setTableView()
        
        self.view.backgroundColor = Constant.bgViewColor
    }
    
    func addOrder() {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_zhusuAddDingdan(dic: self.param), modelClass: SGBaseModel<DYZSDetailModel>.self) {[weak self] (res) in
            if let `self` = self {
                let vc = DYZhifuViewController(id:614)
                self.sy_push(vc)
            }
            
        } Error: {
            
        }
    }
    
    func setBottomPrice() {
        let cost = Double(model.roomList?[self.selectIndex].rates ?? "0") ?? 0.0
        let zongjia = cost * Double(self.roomNum * self.dayNum!)
        let zongjiaStr = String(format: "%.2f", zongjia)
        bottom.configure(zongjiaStr, "0.0")
    }
    
    func setupTopView() {
        view.addSubview(renyang)
        renyang.addSubview(titleLb)
        
        renyang.addSubview(back)
        back.addAction {[weak self] in
            if let `self` = self {
                self.popVC()
            }
        }
        self.backSnp(back)
        renyang.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(173.autoWidth())
        }
        titleLb.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(13.autoWidth() + STATUSBAR_HEIGHT)
            make.height.equalTo(17.autoWidth())
        }
        
    }
    
    func setAlert() {
        alert.currentVC = self
        alert.configure(self.model, self.selectIndex, self.selectTime, self.dayNum)
        self.view.addSubview(alert)
        alert.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-56.autoWidth()-BOTTOM_SAFE_HEIGHT)
            make.top.left.right.equalToSuperview()
        }
    }

    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 15.autoWidth(), y: NAV_HEIGHT , width: SCREEN_WIDTH-30.autoWidth(), height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT-56.autoWidth()), style:.grouped)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = .clear
        self.tableView!.register(DYJDInfoCell.self, forCellReuseIdentifier: "DYJDInfoCell")
        self.tableView!.register(DYJDBiduCell.self, forCellReuseIdentifier: "DYJDBiduCell")
        self.tableView!.register(DYJDYudingInfoInputCell.self, forCellReuseIdentifier: "DYJDYudingInfoInputCell")
        self.tableView!.register(DYJDFapiaoCell.self, forCellReuseIdentifier: "DYJDFapiaoCell")
        self.tableView!.register(DYJDFeiyongCell.self, forCellReuseIdentifier: "DYJDFeiyongCell")
        self.tableView!.register(DYJDTipCell.self, forCellReuseIdentifier: "DYJDTipCell")
        
        
        self.view.addSubview(self.tableView!)
        self.setAlert()
        bottom = DYYuyueBottomView(frame: CGRect(x: 0, y: SCREEN_HEIGHT - 56.autoWidth() - BOTTOM_SAFE_HEIGHT, width: SCREEN_WIDTH, height: 56.autoWidth()))
        bottom.currentVC = self
        setBottomPrice()
        bottom.clickedClosure = {[weak self] _ in
            if let `self` = self {
                self.addOrder()
            }
        }
        self.view.addSubview(bottom)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0{
            
            let cell: DYJDInfoCell? = tableView.dequeueReusableCell(withIdentifier: "DYJDInfoCell") as? DYJDInfoCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if nil != self.model {
                
                cell?.configure(model, selectIndex, selectTime)
            }
            
            return cell!
        } else if indexPath.row == 1{
            
            let cell: DYJDBiduCell? = tableView.dequeueReusableCell(withIdentifier: "DYJDBiduCell") as? DYJDBiduCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if nil != self.model {
                
                cell?.configure(model)
            }
            
            return cell!
        } else if indexPath.row == 2{
            let cell: DYJDYudingInfoInputCell? = tableView.dequeueReusableCell(withIdentifier: "DYJDYudingInfoInputCell") as? DYJDYudingInfoInputCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if nil != self.model {
                
                cell?.configure(model)
            }
            
            return cell!
        } else if indexPath.row == 3{
            
            let cell: DYJDFapiaoCell? = tableView.dequeueReusableCell(withIdentifier: "DYJDFapiaoCell") as? DYJDFapiaoCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if nil != self.model {
                
                
            }
            
            return cell!
        } else if indexPath.row == 4{
            
            let cell: DYJDFeiyongCell? = tableView.dequeueReusableCell(withIdentifier: "DYJDFeiyongCell") as? DYJDFeiyongCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if let m = self.model {
                
                cell?.configure(m, self.selectIndex, self.selectTime, self.dayNum)
            }
            
            return cell!
        }else {
            
            let cell: DYJDTipCell? = tableView.dequeueReusableCell(withIdentifier: "DYJDTipCell") as? DYJDTipCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if let m = self.model {
                
                cell?.configure(m, self.selectIndex, self.dayNum)
            }
            
            return cell!
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt")
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let v = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.01))
        return v
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.01))
        return v
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
}
